package com.cuiweiyou.gdx_1_9_10.actor;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class DemoActor2 extends Actor {
    private TextureRegion region;
    private Body body;

    public DemoActor2(String assets, World world, SpriteBatch batch) {
        region = new TextureRegion(new Texture(assets));
        setSize(this.region.getRegionWidth(), this.region.getRegionHeight());


        BodyDef bodyDef = new BodyDef();
        // 默认是Static Body，只有定义Dynamic 才会受重力影响
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(200, 1200);
        body = world.createBody(bodyDef);

        PolygonShape polygonShape = new PolygonShape();
        polygonShape.setAsBox(getWidth() / 2, getHeight() / 2);
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = polygonShape;
        fixtureDef.density = 2;

        body.createFixture(fixtureDef);
        setPosition(200, 1200);

        polygonShape.dispose();
    }

    public Body getBody() {
        return body;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(region,       //
                getX(),       //
                getY(),       //
                getOriginX(), //
                getOriginY(), //
                getWidth(),   //
                getHeight(),  //
                getScaleX(),  //
                getScaleY(),  //
                getRotation());
    }

    public void render() {
        //setPosition(getX()-10,getY()-10);
        setPosition(body.getPosition().x, body.getPosition().y);
    }
}
